var san = ''
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bgsan: san,
    gender: ['男性', '女性'], // 性别
  },
  // 选择性别
  chooseGender(e) {
    console.log(e.detail.value)
    let index = e.detail.value
    let _t = this
    _t.setData({
      youGender: _t.data.gender[index]
    })
  },
  // 选择行政区
  chooseRegion(e) {
    console.log(e.detail.value)
    let index = e.detail.value
    let _t = this
    _t.setData({
      youArea: _t.data.area[index]
    })
  },
  // 选择团队
  chooseTeam(e) {
    console.log(e.detail.value)
    let index = e.detail.value
    let _t = this
    let teamList = _t.data.teamList

    
    _t.setData({
      youTeam: _t.data.teamList[index].model_name,
      youTeamId: _t.data.teamList[index].id,
    })
  },
  // 选择爱好
  chooseHobby(e) {
    wx.navigateTo({
      url: '/user_personal/hobby/hobby',
    })
  },
  // 填写信息
  fillInForm(e) {
    console.log(e.detail.value)
    // 爱好
    let hobby = wx.getStorageSync("hobby")
    let realhobby = []
    for (let i = 0; i < hobby.length;i++){
      realhobby.push(hobby[i].hobby_name)
    }
    realhobby = realhobby.join(',')
    let _t = this

    let v = e.detail.value
    let uSex = _t.data.youGender
    let uName = v.uName
    let uAge = v.uAge
    // let uTeam = _t.data.youTeamId
    let uTeam = ''

    // 直接判断之前是不是有队伍    
    if (_t.data.userInfo.area_model_name != '' || _t.data.userInfo.area_model_name != undefined || _t.data.userInfo.area_model_name != null) {
      for (var i = 0; i < _t.data.teamList.length; i++) {
        console.log(_t.data.teamList[i])
        if (_t.data.userInfo.area_model_name == _t.data.teamList[i].model_name) {
          uTeam = _t.data.teamList[i].id
        }
      }
    } else {
      uTeam = _t.data.youTeamId
    }

    if (uName == '' || uSex == undefined || uAge == '' || realhobby == ''){
      wx.showToast({
        title: '信息填写不完整，请继续填写',
        icon: 'none',        
        duration: 2000
      })
      return
    }
    
    getApp().request({
      url: getApp().api.hobby.userEdit,
      method: 'POST',
      data: {
        area_model_id: uTeam,
        realname: uName,
        realsex: uSex,
        realage: uAge,
        realhobby: realhobby
      },
      success: function(t) {
        console.log(t)
        if(t==true){
          _t.setData({
            showUser: !_t.data.showUser
          })
          _t.userInfo(_t)
        }else{
          wx.showToast({
            title: '对不起，请检查信息是否填写完整',
            icon: 'none',            
            duration: 2000
          })
        }

      }
    });

  },
  // 分队列表
  teamList(_t){
    let team = []
    // fendui
    getApp().request({
      url: getApp().api.hobby.fendui,
      success: function (t) {
        console.log(t)
        let teamList = t.data
        for (let i = 0; i < teamList.length;i++){
          team.push(teamList[i].model_name)
        }
        _t.setData({
          team: team,
          teamList: teamList
        })
      }
    });
  },
  // 获取用户信息
  userInfo(_t){
    getApp().request({
      url: getApp().api.hobby.userInfo,
      success: function (t) {
        console.log(t)
        let userInfo = t.data
        if (userInfo.realhobby != null){
          userInfo.realhobby = userInfo.realhobby.split(',')          
        }
        let showUser = ''
        userInfo.realname == null ? (showUser = false) : (showUser=true)
        showUser = showUser
        _t.setData({
            userInfo: userInfo,
          showUser: showUser
        })
      }
    });
  },
  // 显示 修改form
  changenews(e){
    let _t = this
    _t.setData({
      showUser: !_t.data.showUser,
      changeshowUser:true
    })
  },

  onLoad: function(options) {
    let _t = this
    _t.teamList(_t)
    _t.userInfo(_t)

    console.log(wx.getStorageSync("hobby"))
  },

  
  onReady: function() {

  },
  onShow: function() {
    let hobby = wx.getStorageSync("hobby")
    let _t = this
    _t.setData({
      hobby: hobby
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})